package queenChessProblem;

import java.util.LinkedList;
import java.util.List;

import utils.GAretVal;
import genetic_algorithm.Chromosome;
import genetic_algorithm.Crossover;
import genetic_algorithm.FitnessFunction;
import genetic_algorithm.GeneticAlgorithm;
import genetic_algorithm.Mutation;
import genetic_algorithm.Population;
import genetic_algorithm.Selection;

/**
 * Runs the genetic algorithm to solve the queens problem
 */
public class QueensMain {

	private static int MAX_GENS = 500; // maximal number of generations to run
	private static int CROSSOVER_MODE = QueensCrossover.SINGLE_POINT; // the mode of QueensCrossover;
	private static int POP_SIZE = 1000; // number of chromosomes in the population
	private static double CROSSOVER_RATE = 0.95; // chance to perform crossover
	private static double MUTATION_RATE = 0.01; // chance to perform mutation
	private static boolean REMOVE_PARENTS = true; // if chromosomes can be selected only once
	private static boolean ELITE = false; // 
	public static void main(String[] args) {

		// allocate objects to implement each phase of the genetic algorithm
		FitnessFunction fitnessFunction = new QueensFitnessFunction();
		Crossover crossover = new QueensCrossover(CROSSOVER_MODE);
		Selection selector = new QueensSelection(REMOVE_PARENTS);
		Mutation mutation = new QueensMutation();
		
		// create initial population
		List<Chromosome> chromList = new LinkedList<Chromosome>();
		for (int i = 0; i < POP_SIZE; ++i) {
			chromList.add(new QueensChromosome());
		}
		Population population = new Population(chromList, fitnessFunction);

		// initialize the genetic algorithm
		GeneticAlgorithm GA = new GeneticAlgorithm(fitnessFunction, crossover,
				selector, mutation, CROSSOVER_RATE, MUTATION_RATE);

		// find optimal solution
		GAretVal result = GA.execute(population, MAX_GENS, ELITE, 2);
		
		// generate csv data file
		result.createsCsvFiles("QueensProblemResult");
	}
}